package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import control.CFamily;

import model.Family;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class JNewFamily extends javax.swing.JFrame {
	private JLabel lblTitle;
	private JTextField txtName;
	private JScrollPane jScrollPane2;
	private JButton bttCancel;
	private JButton bttSave;
	private JScrollPane jScrollPane1;
	private JTextArea txaDescription;
	private JLabel lblDescription;
	private JTextArea txaObservations;
	private JLabel lblObservations;
	private JLabel lblName;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
		
	public JNewFamily() {
		super();
		initGUI();
		
		bttSave.addActionListener(new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				save();
			}
		});
		
		bttCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
	}
	
	public JNewFamily(Family family, final JListFamily jListFamily) 
	{
		super();
		initGUI();
		
		txtName.setText(family.getName());
		txtName.setEditable(false);
		txaDescription.setText(family.getDescription());
		txaObservations.setText(family.getObservations());
		
		bttSave.addActionListener(new ActionListener() 
		{	
			@Override
			public void actionPerformed(ActionEvent arg0) {
				update();
				jListFamily.loadFamilies();
			}
		});
		
		bttCancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				close();
			}
		});
	}
	
	private void close()
	{
		this.dispose();
	}
	
	private void update()
	{
		try 
		{
			Family family = new Family();
			family.setName(txtName.getText());
			family.setDescription(txaDescription.getText());
			family.setObservations(txaObservations.getText());
			
			if(CFamily.getInstance().update(family))
			{
				JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
				this.close();
			}
			else
				JOptionPane.showMessageDialog(null, "Erro ao realizar o cadastro");
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
	
	}
	
	private void save()
	{
		try 
		{
			Family family = new Family();
			family.setName(txtName.getText());
			family.setDescription(txaDescription.getText());
			family.setObservations(txaObservations.getText());
			
			if(CFamily.getInstance().create(family))
			{
				JOptionPane.showMessageDialog(null, "Cadastro realizado com sucesso");
				close();
			}
			else
				JOptionPane.showMessageDialog(null, "Erro ao realizar o cadastro");
		} catch (SQLException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		} catch (ClassNotFoundException e) 
		{
			JOptionPane.showMessageDialog(null, e.getLocalizedMessage());
		}
	}
	
	private void initGUI() {
		
		try {
			this.setVisible(true);
			this.setPreferredSize(new java.awt.Dimension(419, 434));
			this.setLayout(null);
			this.setSize(419, 434);
			{
				lblTitle = new JLabel();
				this.add(lblTitle);
				lblTitle.setText("New Family");
				lblTitle.setBounds(12, 5, 376, 21);
				lblTitle.setHorizontalAlignment(SwingConstants.CENTER);
			}
			{
				lblName = new JLabel();
				this.add(lblName);
				lblName.setText("Name:");
				lblName.setBounds(12, 43, 46, 21);
			}
			{
				txtName = new JTextField();
				this.add(txtName);
				txtName.setBounds(70, 40, 318, 28);
			}
			{
				lblObservations = new JLabel();
				this.add(lblObservations);
				lblObservations.setText("Observations:");
				lblObservations.setBounds(12, 84, 91, 21);
			}
			{
				jScrollPane1 = new JScrollPane();
				this.add(jScrollPane1);
				jScrollPane1.setBounds(12, 111, 376, 76);
				{
					txaObservations = new JTextArea();
					jScrollPane1.setViewportView(txaObservations);
					txaObservations.setBounds(12, 111, 376, 76);
				}
			}
			{
				lblDescription = new JLabel();
				this.add(lblDescription);
				lblDescription.setText("Description:");
				lblDescription.setBounds(12, 207, 82, 21);
			}
			{
				jScrollPane2 = new JScrollPane();
				this.add(jScrollPane2);
				jScrollPane2.setBounds(12, 234, 376, 82);
				{
					txaDescription = new JTextArea();
					jScrollPane2.setViewportView(txaDescription);
					txaDescription.setBounds(12, 234, 376, 82);
				}
			}
			{
				bttSave = new JButton();
				this.add(bttSave);
				bttSave.setText("Save");
				bttSave.setBounds(12, 337, 175, 28);
			}
			{
				bttCancel = new JButton();
				this.add(bttCancel);
				bttCancel.setText("Cancel");
				bttCancel.setBounds(208, 337, 181, 28);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
